﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDSBDemandControl.Presenter;
using CDSBDemandControl.Model;
using System.Drawing;

namespace CDSBDemandControl.View.Web.PaginasInternas
{
    public partial class ManagementTeamTasks : System.Web.UI.Page, IDiscriminatorDemand
    {
        private Presenter.DiscriminatorDemand Presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            if (!this.IsPostBack || this.Session["Presenter"] == null)
            {
                this.Presenter = new DiscriminatorDemand(this);

                this.Session["Presenter"] = this.Presenter;

                if (!this.IsPostBack) this.ListManagementTeamTasks();
            }
            else
            {
                this.Presenter = (Presenter.DiscriminatorDemand)this.Session["Presenter"];
                this.Presenter.ResetView(this);
            }

        }

        private void ListManagementTeamTasks()
        {
            this.Presenter.ListManagementTeamTasks();
        }

        void IDiscriminatorDemand.ExibirManagementTeamTasks(List<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            var aux = from WorkItem in demandModels                                            
                      orderby WorkItem.WorkItemType descending, WorkItem.EstimatedStart
                      select WorkItem;

            this.GridManagementTeamTasks.DataSource = (IEnumerable<CDSBDemandControl.Model.DemandModel>)aux;
            this.GridManagementTeamTasks.DataBind();

            (this.Master.FindControl("LblUltimoUpdate") as Label).Text = "Última atualização em: " + this.Presenter.ManagementTeamTasksLastUpdate.ToShortDateString() + " as " + this.Presenter.ManagementTeamTasksLastUpdate.ToShortTimeString();

            this.ExibirObservacao("Gestão de Desenvolvimento");
            this.CriarLegenda();
        }

        private void CriarLegenda()
        {
            this.LgdFormatoLATE.Text = "Data - Responsável";
            this.LgdFormatoDEADLINE.Text = "Data - Responsável";
            this.LgdFormatoDEADLINECOMMING.Text = "Data - Responsável";
            this.LgdFormatoAWAY.Text = "Data - Responsável";
            

            this.LgdDescLATE.Text = "Problema no planejamento ou na execução do fluxo;";
            this.LgdDescDEADLINE.Text = "Alerta, data de início estimado próxima e tarefa não atribuida a nenhum desenvolvedor;";
            this.LgdDescDEADLINECOMMING.Text = "Atenção, se até este momento não há confirmação da chegada da tarefa, a mesma deve ser replanejada;";
            this.LgdDescAWAY.Text = "Tarefa aguardando atribuição ao desenvolvedor;";            
            
        }

        private void ExibirObservacao(string obs)
        {
            this.LblObsOrdenacao.Text = obs;
            this.LblObsOrdenacao.Visible = this.LblObsOrdenacao.Text.Length > 0 ? true : false;
        }

        protected void Grid_DataBinding(object sender, EventArgs e)
        {
            ((GridView)sender).Visible = true;
        }

        protected void GridManagementTeamTasks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DemandModel current = ((CDSBDemandControl.Model.DemandModel)(e.Row.DataItem));
                
                //if(current.EstimatedConclude.HasValue && current.EstimatedConclude.Value.Date < DateTime.Now.Date)
                    //e.Row.Cells[6].BackColor = Color.Red;
            }

        }

        private int i = 0;
        public string formatTitle(object title, object description)
        {
            i++;
            string id = i + title.ToString().ToLower().Trim().Replace(" ", "").Replace("-", "").Replace("á", "").Replace("ç", "").Replace("(", "").Replace(")", "").Replace("ã", "").Replace("õ", "");
            string content = description == null ? null : description.ToString();

            string link = "<span onclick=\"javascript:{ var aux =  document.getElementById('" + id + "'); var auxLink =  document.getElementById('span" + id + "'); if(aux.style.display == 'none') {aux.style.display = 'block'; auxLink.innerHTML = '<b>&nbsp;(-)</b>';} else {aux.style.display = 'none'; auxLink.innerHTML = '<b>&nbsp;(+)</b>';}}\"><span id='" + "span" + id + "' style='cursor:hand; color:red;'><b> (+)</b></span><b>" + title + "</b></span>";

            string div = link + @"<div id='" + id + "' style='display:none; position:relative;'><i>" + content + "</i></div>";

            if (string.IsNullOrEmpty(content))
                return "<b>" + title + "</b>";
            else
                return div;
        }

        #region IDiscriminatorDemand Members

        void IDiscriminatorDemand.ExibirFila(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirEsteira(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirRanking(IEnumerable<CDSBDemandControl.Model.DeveloperModel> developers, IEnumerable<CDSBDemandControl.Model.DeveloperModel> leaders, IEnumerable<CDSBDemandControl.Model.DeveloperModel> focalpoints)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAtrasos(IEnumerable<CDSBDemandControl.Model.DemandModel> PendingDemands, IEnumerable<CDSBDemandControl.Model.DemandModel> Concludeddemands)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFaturamento(List<CDSBDemandControl.Model.BillingModel> billing, bool canSeeBilling)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirScenariosAndTasks(List<CDSBDemandControl.Model.ScenarioModel> scenarios)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirCodeReview(IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithCodeReview, IEnumerable<CDSBDemandControl.Model.DemandModel> WorkItemsWithoutCodeReview)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirListaDisp(IEnumerable<CDSBDemandControl.Model.DemandModel> list, IEnumerable<CDSBDemandControl.Model.DeveloperModel> modelAusencias, List<DateTime> fer)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirBugsdeProducaoUltimoMes(List<CDSBDemandControl.Model.DemandModel> BugList)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryWaitingList(List<CDSBDemandControl.Model.BillingModel> Billing, bool AllowsShow)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirAlocacao(IEnumerable<CDSBDemandControl.Model.DemandModel> demandModels)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirFactoryProductionList(List<CDSBDemandControl.Model.BillingModel> Billing, bool AllowsShow)
        {
            throw new NotImplementedException();
        }

        void IDiscriminatorDemand.ExibirSolicitacoesSites(List<CDSBDemandControl.Model.DemandModel> listaView)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
